<?php

namespace App\Http\Controllers\Admin;

use App\Models\ActivityPage;
use App\Models\Goods;
use App\Services\CommonService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

//九大分类管理:办公设备、电脑平板、品牌手机、能源设备、健康理疗、游戏设备、智能家居、开店赋能、数码摄影
//活动专区管理:花呗专区、热租爆品、短租精选、租完即送
class ActivityManageController extends BaseController
{
    //获取活动页 http://shop.cn/Admin/ActivityManage/getActivityPage
    public function getActivityPage()
    {
        $activityPage = ActivityPage::GetActivityPage();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $activityPage);
    }

    //根据id获取活动页(预览) http://shop.cn/Admin/ActivityManage/getActivityPageById?id=
    // grant all privileges on *.* to huaxiaozu@'%' with grant option; #授权
    public function getActivityPageById(Request $request)
    {  //
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        $data = ActivityPage::GetOneActivityPage(array('id' => $input['id']));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //编辑活动页POST http://shop.cn/Admin/ActivityManage/editActivityPage  {"id":"","level":"","main_title":"","goodsidStr":""}
    public function editActivityPage(Request $request)
    {
        $input = $request->input();
        if (empty($input['id']) || empty($input['level']) || empty($input['main_title']) || empty($input['goodsidStr'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '参数为空');
        }
        $activityPage = ActivityPage::GetOneActivityPage(array('id' => $input['id']));
        $goodsidStr = $input['goodsidStr'];
        $goodsidArr = explode(',', $input['goodsidStr']);
        $select = array('g.id as goods_id', 'c.name as cate_name', 'g.title', 'g.imgurl', 'g.new_grade', 'g.rent_label', 'g.is_huabei', 'g.min_price');
        $goods = Goods::from('sp_goods_2 as g')
            ->leftjoin('sp_category as c', 'c.id', 'g.category_id')
            ->select($select)
            ->whereIn('g.id', $goodsidArr)
            ->orderByRaw(DB::raw("FIELD(g.id,$goodsidStr)"))
            ->get();
        $goods = $goods ? $goods->toArray() : array();
        $details = $activityPage['details'];
        if (empty($details)) {
            $details_arr = array('first_level' => array('main_title' => '', 'goods' => ''), 'second_level' => array('main_title' => '', 'goods' => ''), 'third_level' => array('main_title' => '', 'goods' => ''));
        } else {
            $details_arr = json_decode($details, true);
        }
        if ($input['level'] == 'first_level') {
            $details_arr['first_level']['main_title'] = $input['main_title'];
            $details_arr['first_level']['goods'] = $goods;
        } elseif ($input['level'] == 'second_level') {
            $details_arr['second_level']['main_title'] = $input['main_title'];
            $details_arr['second_level']['goods'] = $goods;
        } elseif ($input['level'] == 'third_level') {
            $details_arr['third_level']['main_title'] = $input['main_title'];
            $details_arr['third_level']['goods'] = $goods;
        }
        $details = json_encode($details_arr);
        ActivityPage::editDataById(array('id' => $input['id']), array('details' => $details, 'update_time' => date('Y-m-d H:i:s')));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '保存成功');
    }

    //发布POST http://shop.cn/Admin/ActivityManage/release  {"id":""}
    public function release(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        ActivityPage::editDataById(array('id' => $input['id']), array('status' => ActivityPage::ACTIVITY_PAGE_STATUS_PUBLISHED, 'update_time' => date('Y-m-d H:i:s')));
        //设置缓存
        $activityPage = ActivityPage::GetOneActivityPage(array('id' => $input['id']));
        Redis::hset(ActivityPage::ACTIVITY_PAGE_KEY, $input['id'], json_encode($activityPage));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '发布成功');
    }


}
